<!--
 * @Author: your name
 * @Date: 2022-03-31 17:18:57
 * @LastEditTime: 2022-03-31 17:37:57
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \geyao\geyao\浅拷贝实现方式1assign220331.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>浅拷贝实现方式assign</title>
</head>

<body>
  <script>
    //浅拷贝是创建一个新对象，这个对象有着原始对象属性值的一份精确拷贝。如果属性是基本类型，
    //拷贝的就是基本类型的值，如果属性是引用类型，拷贝的就是内存地址 ，所以如果其中一个对象改变了这个地址，就会影响到另一个对象
    //任意多个的源对象自身的可枚举属性拷贝给目标对象，然后返回目标对象
    let obj1 = {
      person: {
        name: "geyao",
        age: 18
      },
      sports: 'basketball'
    };
    let obj2 = Object.assign({}, obj1);
    obj2.person.name = "fangfang";
    obj2.sports = 'football'
    console.log(obj1); // { person: { name: '小红', age: 18 }, sports: 'basketball' }
    console.log(obj2); // { person: { name: '小红', age: 18 }, sports: 'football' }
    obj1.person.name = "小红";
    console.log(obj1);//{ person: { name: '小红', age: 18 }, sports: 'basketball' }
    console.log(obj2); // { person: { name: '小红', age: 18 }, sports: 'football' }
  </script>
</body>

</html>